<?php #Корзина
/* if(empty($_POST))
	$_POST=$_GET;
	$elog=false;
	$conn->echoQ=true; */
	
	ini_set( 'display_errors', 1 );
	ini_set( 'display_startup_errors', 1 );
	error_reporting( E_ALL );

	$action	= isset( $_POST['action'] )	? $_POST['action']	: '';

	switch ( $action )
	{
		case 'select_models':
			$brand 					= isset( $_POST['brand'] )	? $_POST['brand']	: '';
			$models_html			= '<option value="">Выберите модель</option>';	
			$models_ui_select_html	= '<option value="">Выберите значение</option>';	
			
			
			
			$conn->query("SELECT product_code, name FROM store_products_cache where parents = '#5##39#' and brand = '{$brand}'");
			
			while( $row = $conn->fetch() ) {
				$models_html .= '<option value="'.$row->product_code.'">'.$row->name.'</option>';
				$models_ui_select_html .=  '<option value="'.$row->product_code.'">'.$row->name.'</option>';
			}
			echo json_encode( array( 'models_ui_select_html' => $models_ui_select_html , 'models_html' => $models_html ) );
		break;
		
		case 'select_accessories':
		
		$bag_title = $filter_title = $pr_html_bag = $pr_html_filter = $pr_html_nozzle = $pr_html_tube = '';
		$pr_bag_codes = $pr_filter_codes = $pr_nozzle_codes = $pr_tube_codes = '';
		$bag_title = $filter_title = $nozzle_title = $tube_title = '';
		$pr_bag_codes_for_brand = $pr_filter_codes_for_brand = $pr_nozzle_codes_for_brand = $pr_tube_codes_for_brand = '';

		$model_code 	= $_POST['model_val'];
		$model_name  	= isset( $_POST['model_name'] ) ? $_POST['model_name']	: '';
		$brand			= isset( $_POST['brand'] )	? $_POST['brand']	: '';
		
		//вначале ищем аксессуары именно для указанной модели
		$conn->query("SELECT access_code, product_code, type, brand FROM definition_accessories");
		

		while( $row = $conn->fetch() ) {
			if( preg_match( '/'.$model_code.',?/', $row->product_code ) ) {
				if( $row->type == 'bag' ) {
					$pr_bag_codes .= $row->access_code.',';
					continue;	
				}
				if( $row->type == 'filter' ) {
					$pr_filter_codes .= $row->access_code.',';
					continue;	
				}
				if( $row->type == 'nozzle' ) {
					$pr_nozzle_codes .= $row->access_code.',';
					continue;	
				}
				if( $row->type == 'tube' ) {
					$pr_tube_codes .= $row->access_code;
					continue;	
				}
				continue;
			}
			elseif( preg_match( '('.$brand.',?/i)', $row->brand ) || $row->brand == 'universal' ) {
				if( $row->type == 'bag' ) {
					$pr_bag_codes_for_brand .= $row->access_code.',';
					continue;	
				}
				if( $row->type == 'filter' ) {
					$pr_filter_codes_for_brand .= $row->access_code.',';
					continue;	
				}
				if( $row->type == 'nozzle' ) {
					$pr_nozzle_codes_for_brand .= $row->access_code.',';
					continue;	
				}
				if( $row->type == 'tube' ) {
					$pr_tube_codes_for_brand .= $row->access_code.',';
					continue;	
				}
				continue;
			}
		}

		$pr_bag_codes 					= substr( $pr_bag_codes, 0, -1 );
		$pr_filter_codes 				= substr( $pr_filter_codes, 0, -1 );
		$pr_nozzle_codes 				= substr( $pr_nozzle_codes, 0, -1 );
		$pr_tube_codes 					= substr( $pr_tube_codes, 0, -1 );
		
		$pr_bag_codes_for_brand 		= substr( $pr_bag_codes_for_brand, 0, -1 );
		$pr_filter_codes_for_brand 		= substr( $pr_filter_codes_for_brand, 0, -1 );
		$pr_nozzle_codes_for_brand 		= substr( $pr_nozzle_codes_for_brand, 0, -1 );
		$$pr_tube_codes_for_brand 		= substr( $pr_tube_codes_for_brand, 0, -1 );

			
		$bag_title		= ($pr_bag_codes != '' ) ? 'Пылесборники для модели '.$brand .' '.$model_name : 'Пылесборники для брэнда '.$brand;
		$filter_title 	= ($pr_filter_codes != '') ? 'Фильтры для модели '.$brand .' '.$model_name : 'Фильтры для брэнда '.$brand;
		$nozzle_title 	= ($pr_nozzle_codes != '') ? 'Насадки для модели '.$brand .' '.$model_name : 'Насадки для брэнда '.$brand;
		$tube_title 	= ($pr_tube_codes != '') ? 'Удлинительные трубки для модели '.$brand .' '.$model_name : 'Удлинительные трубки для брэнда '.$brand;
		
		$pr_bag_codes		= ( $pr_bag_codes == '' ) ? $pr_bag_codes_for_brand : $pr_bag_codes;
		$pr_filter_codes	= ( $pr_filter_codes == '' ) ? $pr_filter_codes_for_brand : $pr_filter_codes;
		$pr_nozzle_codes	= ( $pr_nozzle_codes == '' ) ? $pr_nozzle_codes_for_brand : $pr_nozzle_codes;
		$pr_tube_codes		= ( $pr_tube_codes == '' ) ? $pr_tube_codes_for_brand : $pr_tube_codes;

		if( $pr_bag_codes != '' ) {
			$params = array(
					'where'		=> "product_code in({$pr_bag_codes})",
					'order'		=> 'find_in_set(`pp`, "1,2,3,4,5,0")',
				);

			$products = getProductList( $params );

			foreach( $products['list'] as $product ) {
				$pr_html_bag .= $load->block( 'product', array( 'product' => $product ) );
			}
		}
		if( $pr_filter_codes != '' ) {
			$params = array(
					'where'		=> "product_code in({$pr_filter_codes})",
					'order'		=> 'find_in_set(`pp`, "1,2,3,4,5,0")',
				);

			$products = getProductList( $params );

			foreach( $products['list'] as $product ) {
				$pr_html_filter .= $load->block( 'product', array( 'product' => $product ) );
			}
		}
		if( $pr_nozzle_codes != '' ) {
			$params = array(
					'where'		=> "product_code in({$pr_nozzle_codes})",
					'order'		=> 'find_in_set(`pp`, "1,2,3,4,5,0")',
				);

			$products = getProductList( $params );

			foreach( $products['list'] as $product ) {
				$pr_html_nozzle .= $load->block( 'product', array( 'product' => $product ) );
			}
		}
		if( $pr_tube_codes != '' ) {
			$params = array(
					'where' => "product_code in({$pr_tube_codes})",
					'order' => 'find_in_set(`pp`, "1,2,3,4,5,0")',
				);

			$products = getProductList( $params );

			foreach( $products['list'] as $product ) {
				$pr_html_tube .= $load->block( 'product', array( 'product' => $product ) );
			}
		}

		echo json_encode (
			array( 
				'pr_html_bag'		=> $pr_html_bag, 
				'pr_html_filter'	=> $pr_html_filter, 
				'pr_html_nozzle'	=> $pr_html_nozzle, 
				'pr_html_tube'		=> $pr_html_tube, 
				'bag_title'			=> $bag_title, 
				'filter_title'		=> $filter_title, 
				'nozzle_title'		=> $nozzle_title,
				'tube_title'		=> $tube_title
			)
		);
			
		break;
	}